package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Permission;
import com.atguigu.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @ClassName PermissionController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-08-30 19:18
 * @Version 1.0
 */
@Controller
@RequestMapping("/permission")
public class PermissionController extends BaseController {

    @Reference
    private PermissionService permissionService;

    //跳转index页面
    @PreAuthorize("hasAuthority('permission.show')")
    @RequestMapping
    public String toIndex(Model model){
        List<Permission> list =  permissionService.findAllMenu();
        model.addAttribute("list",list);
        return "permission/index";
    }

    //'/permission/create?parentId='+parentId+'&type='+type+'&parentName='+parentName,'新增'
    @PreAuthorize("hasAuthority('permission.create')")
    @RequestMapping("/create")
    public String create(Model model,Permission permission){
        model.addAttribute("permission",permission);
        return "permission/create";
    }

    //保存新增  @{/permission/save}
    @PreAuthorize("hasAuthority('permission.create')")
    @RequestMapping("/save")
    public String save(Permission permission){
        permissionService.insert(permission);
        return "common/successPage";
    }

    // '/permission/edit/' + id,'修改'
    @PreAuthorize("hasAuthority('permission.edit')")
    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable Long id){
        Permission permission = permissionService.getById(id);
        model.addAttribute("permission",permission);
        return "permission/edit";
    }

    //保存修改  @{/permission/update}
    @PreAuthorize("hasAuthority('permission.edit')")
    @RequestMapping("/update")
    public String update(Permission permission){
        permissionService.update(permission);
        return "common/successPage";
    }

    //'/permission/delete/'+id
    @PreAuthorize("hasAuthority('permission.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        permissionService.delete(id);
        return "redirect:/permission";
    }
}
